package com.jorado.etl.service;

import com.jorado.basedata.BaseDataUtils;
import com.jorado.core.utility.IOUtils;
import com.jorado.core.utility.StringUtils;
import com.jorado.etl.model.ApplyEtlInput;
import com.jorado.etl.model.TagInput;
import com.jorado.etl.model.TagResult;
import com.jorado.etl.repository.ESPositionRepository;
import com.jorado.etl.util.RedisUtils;
import com.jorado.event.EventClient;
import com.jorado.fasttext.FilterManager;
import com.jorado.json.JsonUtils;
import com.jorado.lexicon.model.NatureUnit;
import com.jorado.lexicon.postag.PosTagger;

import java.util.*;

/**
 * 职位语料提取服务
 * 转换，过滤，分词
 */
public class ApplyEtlService {

    private String outputFile;
    private FilterManager filterManager;
    private FastTextTagService tagService;
    private ESPositionRepository esPositionRepository;
    private Map<String, Map<String, Object>> fileData;
    private RedisUtils redisUtils;

    public ApplyEtlService(String outputFile) {
        this.outputFile = outputFile;
        this.filterManager = new FilterManager();
        this.tagService = new FastTextTagService();
        this.esPositionRepository = new ESPositionRepository("position_label");
        //this.initFileData("d://es_tag.txt");
        this.redisUtils = new RedisUtils("175.63.101.102");
        //this.initRedisData("d://es_tag.txt");
    }

    public void save(HashSet<ApplyEtlInput> etlInputList) {

        try {

            List<String> outputList = new ArrayList<>();

            for (ApplyEtlInput input : etlInputList) {

                List<String> labels = new ArrayList<>();
//                if (!input.getJobType().equals("其他")) {
//
//                    labels.add("__label__" + StringUtils.padLeft(input.getJobTypeId(), 3, '0') + " ");
//                }

                TagResult tagResult = tag1(input);
                if (tagResult == null) continue;

                for (String jobType : tagResult.getLabel()) {
                    String label = "__label__" + jobType;
                    if (!labels.contains(label)) {
                        labels.add(label);
                    }
                }


                if (labels.size() == 0) continue;

                StringBuilder sb = new StringBuilder();

                sb.append(input.getMajorId()).append(" ");
                sb.append(input.getSchoolId()).append(" ");

//                String major = filter(input.getMajor());
//                String school = filter(input.getSchool());
//
//                sb.append(major).append(" ");
//                sb.append(school).append(" ");
//                sb.append(segment(major)).append(" ");
//                sb.append(segment(school)).append(" ");

                String line = sb.toString();
                if (StringUtils.isNullOrWhiteSpace(line))
                    continue;

                outputList.add(StringUtils.joinString(labels, " ") + " " + line);
            }
            IOUtils.appendLines(this.outputFile, outputList);
        } catch (Exception ex) {
            EventClient.getDefault().asyncSubmitException("Save etl output data error", ex);
        }
    }

    public TagResult getJobType(ApplyEtlInput input) {

        String jobTypeId;
        String jobTitle;
        String jobType;
        String jobDescription;
        String jobNum;
        String jobId;

        Map<String, Object> doc = esPositionRepository.get(input.getJobNum());
        if (doc == null || doc.size() < 1) return null;

        jobId = doc.get("job_id").toString();

        jobNum = doc.get("job_num").toString();

        jobTypeId = doc.get("job_type").toString();

        jobType = BaseDataUtils.getSubJobTypeName(jobTypeId);

        jobTitle = doc.get("job_title").toString();

        jobDescription = doc.get("job_desc").toString();

        if (StringUtils.isNullOrWhiteSpace(jobDescription) || jobType.equals("其他") || jobTitle.contains("测试数据") || jobTitle.contains("test") || jobTitle.contains("测试职位"))
            return null;

        TagInput tagInput = new TagInput();
        tagInput.setJobId(jobId);
        tagInput.setJobNum(jobNum);
        tagInput.setJobTypeId(jobTypeId);
        tagInput.setJobType(jobType);
        tagInput.setJobTitle(jobTitle);
        tagInput.setJobDescription(jobDescription);

        TagResult tagResult = tagService.doTag(tagInput, 3);

        return tagResult;
    }

    public TagResult tag(ApplyEtlInput input) {

        String jobTypeId;
        String jobTitle;
        String jobType;
        String jobDescription;
        String jobNum;
        String jobId;

        Map<String, Object> doc = esPositionRepository.get(input.getJobNum());
        if (doc == null || doc.size() < 1) return null;

        jobId = doc.get("job_id").toString();

        jobNum = doc.get("job_num").toString();

        jobTypeId = doc.get("job_type").toString();

        jobType = BaseDataUtils.getSubJobTypeName(jobTypeId);

        jobTitle = doc.get("job_title").toString();

        jobDescription = doc.get("job_desc").toString();

        if (StringUtils.isNullOrWhiteSpace(jobDescription) || jobType.equals("其他") || jobTitle.contains("测试数据") || jobTitle.contains("test") || jobTitle.contains("测试职位"))
            return null;

        TagInput tagInput = new TagInput();
        tagInput.setJobId(jobId);
        tagInput.setJobNum(jobNum);
        tagInput.setJobTypeId(jobTypeId);
        tagInput.setJobType(jobType);
        tagInput.setJobTitle(jobTitle);
        tagInput.setJobDescription(jobDescription);

        TagResult tagResult = tagService.doTag(tagInput, 3);

        return tagResult;
    }

    public TagResult tag1(ApplyEtlInput input) {

        Map<String, Object> doc = esPositionRepository.get(input.getJobNum());
        if (doc == null || doc.size() < 1) return null;

        String jobType = doc.get("job_type").toString();

        String jobTypeId = doc.get("job_type_id").toString();

        String jobTitle = doc.get("job_title").toString();

        String label = doc.get("label").toString();

        String jobDescription = doc.get("job_desc").toString();

        if (StringUtils.isNullOrWhiteSpace(jobDescription) || jobType.equals("其他") || jobTitle.contains("测试数据") || jobTitle.contains("test") || jobTitle.contains("测试职位"))
            return null;

        String[] tmps = StringUtils.splitString(label, "|", true);

        List<String> labels = new ArrayList<>();

        for (String s : tmps) {
            labels.add(s);
            if (labels.size() == 3) {
                break;
            }
        }

        TagResult tagResult = new TagResult();
        labels.add(jobTypeId);
        tagResult.setLabel(labels);

        return tagResult;
    }

    public TagResult tag2(ApplyEtlInput input) {

        Map<String, Object> doc = this.fileData.get(input.getJobNum());
        if (doc == null || doc.size() < 1) return null;

        String jobTitle = doc.get("job_title").toString();

        String label = doc.get("label").toString();

        if (StringUtils.isNullOrWhiteSpace(label) || jobTitle.contains("测试数据") || jobTitle.contains("test") || jobTitle.contains("测试职位"))
            return null;

        String[] tmps = StringUtils.splitString(label, "|", true);

        List<String> labels = new ArrayList<>();

        for (String s : tmps) {
            labels.add(s);
            if (labels.size() == 3) {
                break;
            }
        }

        TagResult tagResult = new TagResult();
        tagResult.setLabel(labels);

        return tagResult;
    }

    public TagResult tag3(ApplyEtlInput input) {

        String label = this.redisUtils.syncGet(input.getJobNum());
        String jobTitle = this.redisUtils.syncGet(input.getJobNum() + "_title");

        if (StringUtils.isNullOrWhiteSpace(label) || jobTitle.contains("测试数据") || jobTitle.contains("test") || jobTitle.contains("测试职位"))
            return null;

        String[] tmps = StringUtils.splitString(label, "|", true);

        List<String> labels = new ArrayList<>();

        for (String s : tmps) {
            labels.add(s);
            if (labels.size() == 3) {
                break;
            }
        }

        TagResult tagResult = new TagResult();
        tagResult.setLabel(labels);

        return tagResult;
    }

    /**
     * 过滤
     *
     * @param input
     * @return
     */
    public String filter(String input) {
        return filterManager.filter(input);
    }

    /**
     * 分词
     *
     * @param input
     * @return
     */
    public String segment(String input) {
        List<NatureUnit> natureUnits = PosTagger.getDefault().recognition(input);
        StringBuilder sb = new StringBuilder();
        for (NatureUnit natureUnit : natureUnits) {
            String word = natureUnit.getWord();
            //剔除单个字
            if (word.length() > 1) {
                sb.append(natureUnit.getWord());
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    private void initFileData(String filePath) {

        this.fileData = new HashMap<>();
        List<String> lines = IOUtils.readLines(filePath);
        Map<String, Object> t = new HashMap<>();
        for (String s : lines) {
            Map<String, Object> data = JsonUtils.fromJson(s, t.getClass());
            this.fileData.put(data.get("job_num").toString(), data);
        }
        lines = null;

    }

    private void initRedisData(String filePath) {

        List<String> lines = IOUtils.readLines(filePath);
        Map<String, Object> t = new HashMap<>();
        for (String s : lines) {
            Map<String, Object> data = JsonUtils.fromJson(s, t.getClass());
            String jobNum = data.get("job_num").toString();
            redisUtils.syncSet(jobNum, data.get("label").toString());
            redisUtils.syncSet(jobNum + "_title", data.get("job_title").toString());
        }

    }
}
